package com.lighting.smssystem.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lighting.smssystem.aspect.RequireRole;
import com.lighting.smssystem.constrants.UserRole;
import com.lighting.smssystem.model.bo.CompanyList;
import com.lighting.smssystem.model.vo.AddCompanies;
import com.lighting.smssystem.model.vo.CompanyFilter;
import com.lighting.smssystem.model.vo.CompanyFilterNew;
import com.lighting.smssystem.model.vo.CompanyForm;
import com.lighting.smssystem.service.ICompanyService;
import com.lighting.smssystem.utils.BaseResponse;
import com.lighting.smssystem.utils.ResponseUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Author LeeMaster
 *
 * 查询公司信息，导入公司信息，导出公司信息，删除公司信息，收藏公司信息
 */
@RestController("/company")
@RequestMapping("/company")
@Api(
        value = "公司信息接口",
        protocols = "http",
        description = "获取公司信息接口的API"
)
@Slf4j
public class CompanyController {

    @Resource
    ICompanyService companyService;

    @ApiOperation(
            value = "查询公司接口，模糊查询",
            httpMethod = "GET",
            authorizations = @Authorization(
                    "VIP 用户,ADMIN 用户"
            )

    )
    @ApiResponse(code = 200,message = "",response = CompanyList.class)
    @RequireRole(role = {UserRole.VIP,UserRole.ADMIN})
    @GetMapping("")
    public BaseResponse listCompanies(@ApiParam CompanyFilter filter){
        log.info("查询公司信息{}", filter.toString());

        return ResponseUtil.success(
                companyService.queryCompanyListOld(filter)
        );
//        return ResponseUtil.success();
    }

    @ApiOperation(
            value = "查询企业信息",
            httpMethod = "POST",
            authorizations = @Authorization(
            "VIP 用户,ADMIN 用户"
        )
    )
    @RequireRole(role = {UserRole.VIP,UserRole.ADMIN})
    @GetMapping("/one")
    public BaseResponse getCompany(
            @ApiParam @RequestParam("id") Long id
    ) {
        log.info("查询企业信息{}", id);

        return ResponseUtil.success(companyService.company(id));
    }

    @ApiOperation(
            value = "查询公司接口，模糊查询 新的接口 原来设计的有问题",
            httpMethod = "GET",
            authorizations = @Authorization(
                    "VIP 用户,ADMIN 用户"
            )

    )
    @ApiResponse(code = 200,message = "",response = CompanyList.class)
    @RequireRole(role = {UserRole.VIP,UserRole.ADMIN})
    @GetMapping("/new")
    public BaseResponse listCompaniesNew(@ApiParam CompanyFilterNew filter){

        log.info("查询公司信息{}", filter.toString());
        return ResponseUtil.success(
                companyService.queryCompanyList(filter)
        );
    }



    @ApiOperation(
            value = "删除公司接口",
            httpMethod = "DELETE",
            notes = "成功 code 返回 200",
            authorizations = @Authorization(
                    "ADMIN 用户"
            )

    )
    @ApiParam(value = "公司id 列表 逗号分割")
    @DeleteMapping("")
    @RequireRole(role = {UserRole.ADMIN})
    public BaseResponse deleteCompanies(
            @RequestParam("ids")String ids
    ){
        log.info("删除公司列表{}", ids);
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .collect(Collectors.toList());

        return ResponseUtil.success(
                companyService.deleteCompanies(idList)
        );

    }


    @ApiOperation(
            value = "新增公司接口",
            httpMethod = "POST",
            notes = "成功返回200 code",
            authorizations = @Authorization(
                    "ADMIN 用户"
            )

    )
    @ApiParam(value = "公司id 列表 逗号分割")
    @RequireRole(role = {UserRole.ADMIN})
    @PostMapping("")
    public BaseResponse createComanies(
            @RequestBody @Valid AddCompanies companies
    ){
        for (CompanyForm form : companies.getCompanies()) {
            log.info("添加公司信息{}", form.toString());
        }
        companyService.addCompanies(companies);
        return ResponseUtil.success();
    }

    @ApiOperation(
            value = "修改公司接口",
            httpMethod = "PUT",
            notes = "成功返回200 code",
            authorizations = @Authorization(
                    "ADMIN 用户"
            )

    )
    @ApiParam(value = "公司id 列表 逗号分割")
    @RequireRole(role = {UserRole.ADMIN})
    @PutMapping("")
    public BaseResponse updateComanies(
            @RequestBody @Valid List<CompanyForm> companies
    ){
        for (CompanyForm form : companies) {
            log.info("修改接口{}", form.toString());
        }
//        return ResponseUtil.success();
        return ResponseUtil.success(
                companyService.updateCompanies(companies)
        );
    }

}
